﻿using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.BLL.DocumentPrinter;
using System.Linq;
using MS.InformationManagement.BLL.ReceivedDocuments;
using MS.InformationManagement.Support.Enums;
using System.Collections.Generic;
using MS.InformationManagement.DAL.DocumentCenter;
using Microsoft.SharePoint;

namespace MS.InformationManagement.Presenter.DocumentDeliveryBag
{
    public class DocumentDeliveryBagPresenter : BasePresenter<IDocumentDeliveryBagView>
    {

        #region Constructors

        public DocumentDeliveryBagPresenter(IDocumentDeliveryBagView view) : base(view) { }

        #endregion

        #region Public Methods

        public void HandleLoad()
        {
            int totalRecords = 0;
            View.PageIndex = 0;
            View.PageSize = 10;
            View.DeliveryMeans = DocumentDeliveryBagBLL.GetDeliveryMeans().ToList();
            View.DeliverZones = DocumentDeliveryBagBLL.GetDeliveryZones();

            switch (View.ControlType)
            {
                case DeliveryControlType.Reception:
                    var printedSelectedDocuments = DocumentDeliveryBagBLL.GetDeliveryBagDocuments(View.DocNumberSearchCriteria, View.AreaSearchCriteria, View.PageIndex, View.PageSize, out totalRecords, View.SelectedUserCriteria);
                    View.PendingDocuments = printedSelectedDocuments == null ? new List<DocumentoImpreso>() : printedSelectedDocuments.ToList();
                    break;
                case DeliveryControlType.Capture: 
                    var receivedSelectedDocuments = DocumentDeliveryBagBLL.GetDocumentsForCaptureAndWeighUp(View.DocNumberSearchCriteria, View.AreaSearchCriteria, View.PageIndex, View.PageSize, out totalRecords, View.SelectedUserCriteria);
                    View.PendingDocuments = receivedSelectedDocuments == null ? new List<DocumentoImpreso>() : receivedSelectedDocuments.ToList();
                    break;
            }
            View.TotalRecords = totalRecords;
        }

        public void FetchDeliveryPendingDocuments(bool reloadFirstPage)
        {
            int totalRecords = 0;
            View.PageSize = 10;
            if (reloadFirstPage)
                View.PageIndex = 0;
            View.PendingDocuments = DocumentDeliveryBagBLL.GetDeliveryBagDocuments(View.DocNumberSearchCriteria, View.AreaSearchCriteria, View.PageIndex
                , View.PageSize, out totalRecords, View.SelectedUserCriteria).ToList();
            View.TotalRecords = totalRecords;
        }

        public void SaveAction()
        { 
            switch (View.ControlType)
            {
                case DeliveryControlType.Reception: MarkDocumentsAsReceived();
                    break;
            }

            View.ShowMessage("Operación ejecutada exitosamente");
        }

        public void MarkDocumentsAsReceived()
        {
            var operationCommand = new InternalDocumentReceptionCommand(View.SelectedItemsId);
            operationCommand.Execute();
        }

        #endregion
    }
}
